﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using FileTranslator;

namespace FileTranslatorTest
{
    [TestClass]
    public class WholeTableViewWrapperTestscs
    {
        DataTable dt;
        WholeTableViewWrapper dtw;

        [TestInitialize]
        public void Initialize()
        {
            dt = BuildTableHelper.PrepareDataTable3();

            List<DataTableWrapper> lists = new List<DataTableWrapper>();

            dtw = new WholeTableViewWrapper(
                new MockConstraintWithNoConstraintsHelper(), 
                BuildTableHelper.GetTableWrappersFromTable());
            dtw.InnerTable = dt.DefaultView;
        }

        [TestCleanupAttribute]
        public void Cleanup()
        {
        }

        [TestMethod]
        public void TestInsert_Top()
        {
            // 插在表头(同时也是第一个DataTableWrapper的头）
            dtw.InsertAt(0);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "");
            Assert.AreEqual(dr["name"], "");
            Assert.IsTrue(dr["测试"] == "");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[5];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestInsert_Beginning()
        {
            // 插在表头(同时也是第二个DataTableWrapper的头）
            dtw.InsertAt(1);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.AreEqual(dr["name"], "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "");
            Assert.IsTrue(dr["name"] == "");
            Assert.IsTrue(dr["测试"] == "");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[5];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestInsert_Bottom()
        {
            // 插在表头(同时也是第一个DataTableWrapper的头）
            dtw.InsertAt(5);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");

            dr = dt.Rows[5];
            Assert.IsTrue(dr["id"].ToString() == "");
            Assert.AreEqual(dr["name"], "");
            Assert.IsTrue(dr["测试"] == "");
        }

        [TestMethod]
        public void TestInsert_Ending()
        {
            // 插在表头(同时也是第二个DataTableWrapper的尾部）
            dtw.InsertAt(4);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "");
            Assert.AreEqual(dr["name"], "");
            Assert.IsTrue(dr["测试"] == "");

            dr = dt.Rows[5];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestInsert_Mid()
        {
            // 插在表头(同时也是第二个DataTableWrapper的中间）
            dtw.InsertAt(2);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "");
            Assert.AreEqual(dr["name"], "");
            Assert.IsTrue(dr["测试"] == "");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[5];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestDelete_Normal()
        {
            dtw.Delete(2);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestDelete_WrongPosition()
        {
            Assert.IsFalse(dtw.Delete(-1));
            Assert.IsFalse(dtw.Delete(100));
        }

        [TestMethod]
        public void TestMove_CanNotMove()
        {
            // 插在表头(同时也是第二个DataTableWrapper的顶部）
            Assert.IsFalse(dtw.Move(1,-1));
            Assert.IsFalse(dtw.Move(3, 1));

            Assert.IsFalse(dtw.Move(0, -1));
            Assert.IsFalse(dtw.Move(0, 1));
            
            Assert.IsFalse(dtw.Move(4, -1));
            Assert.IsFalse(dtw.Move(4, 1));
        }

        [TestMethod]
        public void TestMove_Down()
        {
            // 插在表头(同时也是第二个DataTableWrapper的中间）
            dtw.Move(2,1);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestMove_Up()
        {
            // 插在表头(同时也是第二个DataTableWrapper的中间）
            dtw.Move(2, -1);

            DataRow dr = dt.Rows[0];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");

            dr = dt.Rows[1];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB3");
            Assert.IsTrue(dr["测试"] == "测试3");

            dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB2");
            Assert.IsTrue(dr["测试"] == "测试2");

            dr = dt.Rows[3];
            Assert.IsTrue(dr["id"].ToString() == "2");
            Assert.IsTrue(dr["name"] == "BB4");
            Assert.IsTrue(dr["测试"] == "测试4");

            dr = dt.Rows[4];
            Assert.IsTrue(dr["id"].ToString() == "3");
            Assert.IsTrue(dr["name"] == "BB5");
            Assert.IsTrue(dr["测试"] == "测试5");
        }

        [TestMethod]
        public void TestChange()
        {
            // 插在表头(同时也是第二个DataTableWrapper的中间）
            dtw.Change(2, dtw.InnerTable[0].Row);

            DataRow dr = dt.Rows[2];
            Assert.IsTrue(dr["id"].ToString() == "1");
            Assert.IsTrue(dr["name"] == "BB1");
            Assert.IsTrue(dr["测试"] == "测试1");
        }
    }
}
